package baidu.reader.common.ui
{
    import baidu.reader.common.*;
    import baidu.reader.common.model.*;
    import baidu.ui.containers.*;
    import baidu.ui.controls.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;
    
    import flash.display.*;
    import flash.events.*;

    public class TopToolBar extends baidu.ui.layout.AutoLayoutContainer
    {
		private var _leftBox:baidu.ui.containers.HBox;
		
		private var _rightBox:baidu.ui.containers.HBox;
		
		private var _tocButton:baidu.ui.controls.Button;
		
		private var _zoomInButton:baidu.ui.controls.Button;
		
		private var _zoomOutButton:baidu.ui.controls.Button;
		
		private var _originalButton:baidu.ui.controls.Button;
		
		private var _settingsButton:baidu.ui.controls.Button;
		
		private var _singlePageButton:baidu.ui.controls.Button;
		
		private var _doublePageButton:baidu.ui.controls.Button;
		
		private var _fullscreenButton:baidu.ui.controls.Button;
		
		private var buttonsArray:Array;
		
		private var tooltipManager:baidu.ui.managers.TooltipManager;
		
		private var screenModel:baidu.reader.common.model.ScreenModel;
		
		private var isCurrentSinglePage:Boolean=true;
		
		private var previousButtonState:Array;
		
		private var _displayTypeDisabled:Boolean;
		
        public function TopToolBar()
        {
            this.previousButtonState = [];
            super();
            this.tooltipManager = baidu.ui.managers.TooltipManager.getInstance();
            this.addLeftBox();
            this.addRightBox();
            this.buttonsArray = [this._tocButton, this._zoomInButton, this._zoomOutButton, this._originalButton, this._settingsButton, this._singlePageButton, this._doublePageButton, this._fullscreenButton];
            var loc1:Tooltip = new baidu.ui.controls.Tooltip();
            loc1.setStyle("skin", ResourceFactory.TipBackground);
            loc1.setStyle("padding", 5);
            this.tooltipManager.defaultRender = loc1;
            this.tooltipManager.register(this._tocButton, " 目录 ");
            this.tooltipManager.register(this._zoomInButton, " 放大 ");
            this.tooltipManager.register(this._zoomOutButton, " 缩小 ");
            this.tooltipManager.register(this._singlePageButton, " 单页 ");
            this.tooltipManager.register(this._doublePageButton, " 双页 ");
            this.tooltipManager.register(this._originalButton, " 还原 ");
            this.tooltipManager.register(this._settingsButton, " 设置 ");
            this.tooltipManager.register(this._fullscreenButton, " 全屏 ");
            this.screenModel = baidu.ui.managers.InstanceManager.createSingletonInstance(baidu.reader.common.model.ScreenModel);
            this.screenModel.addEventListener(flash.events.FullScreenEvent.FULL_SCREEN, this.screenResizedHandler);
            return;
        }

        private function screenResizedHandler(arg1:flash.events.Event):void
        {
            var displayStateStr:String = this.screenModel.stage.displayState;
            switch (displayStateStr)
            {
                case flash.display.StageDisplayState.FULL_SCREEN:
                {
                    this.tooltipManager.register(this._fullscreenButton, " 退出全屏 ");
                    break;
                }
                default:
                {
                    this.tooltipManager.register(this._fullscreenButton, " 全屏 ");
                }
            }
            return;
        }

        private function addLeftBox():void
        {
            this._leftBox = new baidu.ui.containers.HBox();
            this._leftBox.setStyle("gap", 12);
            addChild(this._leftBox);
            var loc1:*;
            loc1 = _childsLayoutInfo[this._leftBox];
            loc1.top = 0;
            loc1.left = 0;
            this._tocButton = new baidu.ui.controls.Button();
            this._tocButton.setStyle("skin", ResourceFactory.ToC_Button_Skin);
			this._tocButton.setStyle("skin_up", ResourceFactory.ToC_Button_Skin_Up);
			this._tocButton.setStyle("skin_down", ResourceFactory.ToC_Button_Skin_Down);
			this._tocButton.setStyle("skin_over", ResourceFactory.ToC_Button_Skin_Over);
			this._tocButton.setStyle("skin_disabled", ResourceFactory.ToC_Button_Skin_Disabled);
            this._tocButton.useHandCursor = true;
            this._tocButton.addEventListener(flash.events.MouseEvent.CLICK, this.tocClickHandler);
            this._leftBox.addChild(this._tocButton);
            this._zoomInButton = new baidu.ui.controls.Button();
            this._zoomInButton.setStyle("skin", ResourceFactory.ZoomIn_Button_Skin);
            this._zoomInButton.useHandCursor = true;
            this._zoomInButton.addEventListener(flash.events.MouseEvent.CLICK, this.zoomInClickHandler);
            this._leftBox.addChild(this._zoomInButton);
            this._zoomOutButton = new baidu.ui.controls.Button();
            this._zoomOutButton.setStyle("skin", ResourceFactory.ZoomOut_Button_Skin);
            this._zoomOutButton.useHandCursor = true;
            this._zoomOutButton.addEventListener(flash.events.MouseEvent.CLICK, this.zoomOutClickHandler);
            this._leftBox.addChild(this._zoomOutButton);
            this._originalButton = new baidu.ui.controls.Button();
            this._originalButton.setStyle("skin", ResourceFactory.Original_Button_Skin);
            this._originalButton.useHandCursor = true;
            this._originalButton.addEventListener(flash.events.MouseEvent.CLICK, this.originalClickHandler);
            this._leftBox.addChild(this._originalButton);
            return;
        }

        private function originalClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.ORIGINAL_SIZE));
            return;
        }

        private function zoomOutClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.ZOOM_IN));
            return;
        }

        private function zoomInClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.ZOOM_OUT));
            return;
        }

        private function bookmarkClickHandler(arg1:flash.events.MouseEvent):void
        {
            return;
        }

        private function tocClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.SHOW_TOC));
            return;
        }

        private function addRightBox():void
        {
            if (this._rightBox)
            {
                this._rightBox.parent.removeChild(this._rightBox);
            }
            this._rightBox = new baidu.ui.containers.HBox();
            this._rightBox.setStyle("gap", 12);
            this._rightBox.setStyle("padding", 12);
            addChild(this._rightBox);
            var loc1:*;
            loc1 = _childsLayoutInfo[this._rightBox];
            loc1.top = 0;
            loc1.right = 0;
            this._settingsButton = new baidu.ui.controls.Button();
            this._settingsButton.setStyle("skin", ResourceFactory.Settings_Button_Skin);
			this._settingsButton.setStyle("skin_up", ResourceFactory.Settings_Button_Skin_Up);
			this._settingsButton.setStyle("skin_over", ResourceFactory.Settings_Button_Skin_Over);
			this._settingsButton.setStyle("skin_down", ResourceFactory.Settings_Button_Skin_Down);
			this._settingsButton.setStyle("skin_disabled", ResourceFactory.Settings_Button_Skin_Disabled);			
            this._settingsButton.useHandCursor = true;
            this._settingsButton.addEventListener(flash.events.MouseEvent.CLICK, this.settingsClickHandler);
			
            this._rightBox.addChild(this._settingsButton);
            this._singlePageButton = new baidu.ui.controls.Button();
            this._singlePageButton.setStyle("skin", ResourceFactory.SinglePage_Button_Skin);
			this._singlePageButton.setStyle("skin_up", ResourceFactory.SinglePage_Button_Skin_Up);
			this._singlePageButton.setStyle("skin_down", ResourceFactory.SinglePage_Button_Skin_Down);
			this._singlePageButton.setStyle("skin_over", ResourceFactory.SinglePage_Button_Skin_Over);
			this._singlePageButton.setStyle("skin_disabled", ResourceFactory.SinglePage_Button_Skin_Disabled);
            this._singlePageButton.useHandCursor = true;
            this._singlePageButton.addEventListener(flash.events.MouseEvent.CLICK, this.singlePageClickHandler);
            this._rightBox.addChild(this._singlePageButton);
			
            this._doublePageButton = new baidu.ui.controls.Button();
            this._doublePageButton.setStyle("skin", ResourceFactory.DoublePage_Button_Skin);
			this._doublePageButton.setStyle("skin_up", ResourceFactory.DoublePage_Button_Skin_Up);
			this._doublePageButton.setStyle("skin_down", ResourceFactory.DoublePage_Button_Skin_Down);
			this._doublePageButton.setStyle("skin_over", ResourceFactory.DoublePage_Button_Skin_Over);
			this._doublePageButton.setStyle("skin_disabled", ResourceFactory.DoublePage_Button_Skin_Disabled);
            this._doublePageButton.useHandCursor = true;
            this._doublePageButton.addEventListener(flash.events.MouseEvent.CLICK, this.doublePageClickHandler);
            this._rightBox.addChild(this._doublePageButton);
            this._fullscreenButton = new baidu.ui.controls.Button();
            this._fullscreenButton.setStyle("skin", ResourceFactory.FullScreen_Button_Skin);
			this._fullscreenButton.setStyle("skin_up", ResourceFactory.FullScreen_Button_Skin_Up);
			this._fullscreenButton.setStyle("skin_down", ResourceFactory.FullScreen_Button_Skin_Down);
			this._fullscreenButton.setStyle("skin_over", ResourceFactory.FullScreen_Button_Skin_Over);
			this._fullscreenButton.setStyle("skin_disabled", ResourceFactory.FullScreen_Button_Skin_Disabled);
            this._fullscreenButton.useHandCursor = true;
            this._fullscreenButton.addEventListener(flash.events.MouseEvent.CLICK, this.fullScreenClickHandler);
            this._rightBox.addChild(this._fullscreenButton);
            return;
        }

        public function disableAll():void
        {
            var i:int;

            var loc1:*;
            i = 0;
            while (i < this.buttonsArray.length)
            {
                try
                {
                    this.previousButtonState[i] = (this.buttonsArray[i] as baidu.ui.controls.Button).enabled;
                    this.disableButton(this.buttonsArray[i] as baidu.ui.controls.Button);
                }
                catch (e:Error)
                {
                    trace(undefined.getStackTrace());
                }
                i = (i + 1);
            }
            return;
        }

        public function enableAll():void
        {
            var loc1:*;
            loc1 = null;
            var loc2:*;
            loc2 = 0;
            var loc3:*;
            loc3 = this.buttonsArray;
            for each (loc1 in loc3)
            {
                this.enableButton(loc1);
            }
            return;
        }

        public function restoreAll():void
        {
            var loc1:*;
            loc1 = 0;
            while (loc1 < this.buttonsArray.length)
            {
                if (this.previousButtonState.length)
                {
                    (this.buttonsArray[loc1] as baidu.ui.controls.Button).enabled = this.previousButtonState[loc1];
                }
                ++loc1;
            }
            return;
        }

        private function disableButton(arg1:baidu.ui.controls.Button):void
        {
            arg1.enabled = false;
            return;
        }

        private function enableButton(arg1:baidu.ui.controls.Button):void
        {
            arg1.enabled = true;
            return;
        }

        private function fullScreenClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.TOGGLE_FULLSCREEN));
            return;
        }

        private function doublePageClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.GO_DOUBLE_PAGE));
            return;
        }

        private function singlePageClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.GO_SINGLE_PAGE));
            return;
        }

        private function settingsClickHandler(arg1:flash.events.MouseEvent):void
        {
            dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.SHOW_SETTING_PANEL));
            return;
        }

        public function get tocButton():baidu.ui.controls.Button
        {
            return this._tocButton;
        }

        public function get zoomInButton():baidu.ui.controls.Button
        {
            return this._zoomInButton;
        }

        public function get zoomOutButton():baidu.ui.controls.Button
        {
            return this._zoomOutButton;
        }

        public function get originalButton():baidu.ui.controls.Button
        {
            return this._originalButton;
        }

        public function get settingsButton():baidu.ui.controls.Button
        {
            return this._settingsButton;
        }

        public function get singlePageButton():baidu.ui.controls.Button
        {
            return this._singlePageButton;
        }

        public function get doublePageButton():baidu.ui.controls.Button
        {
            return this._doublePageButton;
        }

        public function set isSingleDisplay(arg1:Boolean):void
        {
            if (this._displayTypeDisabled)
            {
                return;
            }
            this.isCurrentSinglePage = arg1;
            if (arg1 != true)
            {
                this._singlePageButton.enabled = true;
                this._doublePageButton.enabled = false;
                this._zoomInButton.enabled = false;
                this._zoomOutButton.enabled = false;
                this._originalButton.enabled = false;
            }
            else
            {
                this._singlePageButton.enabled = false;
                this._doublePageButton.enabled = true;
                this._zoomInButton.enabled = true;
                this._zoomOutButton.enabled = true;
                this._originalButton.enabled = true;
            }
            return;
        }

        public function set displayTypeDisabled(arg1:Boolean):void
        {
            this._displayTypeDisabled = arg1;
            if (arg1)
            {
                this._singlePageButton.parent.removeChild(this._singlePageButton);
                this._doublePageButton.parent.removeChild(this._doublePageButton);
            }
            else
            {
                this._rightBox.addChild(this._settingsButton);
                this._rightBox.addChild(this._singlePageButton);
                this._rightBox.addChild(this._doublePageButton);
                this._rightBox.addChild(this._fullscreenButton);
                this.isSingleDisplay = this.isCurrentSinglePage;
            }
            return;
        }

        public function get fullscreenButton():baidu.ui.controls.Button
        {
            return this._fullscreenButton;
        }

        public function get rightBox():baidu.ui.containers.HBox
        {
            return this._rightBox;
        }

    }
}
